package com.chyong.iptv.utils;

import android.content.Context;
import android.content.res.AssetManager;
import com.chyong.iptv.beans.Channel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class DataUtil {

    public static List<Channel> getLocalChannel(String name, Context context) {
        List<Channel> list = new ArrayList<>();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open("data@" + name)));
            String line;
            int id = 0;
            while ((line = bf.readLine()) != null) {
                id++;
                String[] lines = line.split(",");
                Channel channel = new Channel();
                channel.setId(id);
                channel.setName(lines[0]);
                channel.setUrl(lines[1]);
                list.add(channel);
            }
        } catch (IOException e) {
        }
        return list;
    }

    public static List<Channel> DataToChannels(String data) {
        List<Channel> list = new ArrayList<>();
        try {
            BufferedReader bf = new BufferedReader(new StringReader(data));
            String line;
            int id = 0;
            while ((line = bf.readLine()) != null) {
                id++;
                String[] lines = line.split(",");
                Channel channel = new Channel();
                channel.setId(id);
                channel.setName(lines[0]);
                channel.setUrl(lines[1]);
                list.add(channel);
            }
        } catch (IOException e) {
        }
        return list;
    }
}
